package com.springboot.cloud.demos.ribbon.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.SystemErrorType;
import com.springboot.cloud.demos.entity.form.DocumentForm;
import com.springboot.cloud.demos.entity.po.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

import javax.print.Doc;

@Service
public class DocumentService {

    @Autowired
    RestTemplate restTemplate;

    @SentinelResource(fallback = "queryFallback")
    public Result query(String unitId, Integer page) {
        return restTemplate.getForEntity("http://producer/document?unitId={1}&page={2}", Result.class, unitId, page).getBody();
    }

    @SentinelResource(fallback = "queryFallback")
    public Result queryByUnit(String unitId) {
        return restTemplate.getForEntity("http://producer/document/queryByUnit?unitId={1}", Result.class, unitId).getBody();
    }

    public Result queryFallback(String unitId) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result saveFallback(DocumentForm documentForm) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }


    @SentinelResource(fallback = "saveFallback")
    public Result save(@RequestBody DocumentForm form) {
        return restTemplate.postForEntity("http://producer/document", form, Result.class).getBody();
    }

    @SentinelResource(fallback = "queryFallback")
    public Result get(String id) {
        return restTemplate.getForEntity("http://producer/document/{id}", Result.class, id).getBody();

    }
}
